#!/bin/bash

#--------------------------CONFIGURATIONS-----------------------------

# Name of the executable
#executable="cg-stokes"
executable="cg-ns"

# Number of threads
num_threads=1

# Name specifier (used to identify different experiments)
#name_spec="cav-centered-P3-Re3200"
name_spec="p-test"

# Queue: for shared memory, use "smnodes"
queue="smnodes"
#queue="eca"

# Minimum memory requirement
minimum_memory="20000M"

#------------------------END CONFIGURATIONS---------------------------

job_file="qsub_script_to_run_${executable}.sge"
job_name="${executable} ${num_threads} thread(s)"
job_out="out-${executable}-${name_spec}-N${num_threads}.txt"

input_file="temp-${executable}.in-${job_name}-${RANDOM}"
# Make a copy of the input file
cp ${executable}.in "${input_file}"

cat > ${job_file} << EOF
#!/bin/bash
#$ -S /bin/bash

# Select the queue
#$ -q ${queue}
#$ -pe make ${num_threads}

# Memory requirement
#$ -l h_vmem=${minimum_memory}

# Set the job name
#$ -N "${job_name}"

# Set the output file and merge it to the sterr (-j y)
#$ -o ${job_out}
#$ -j y

# Start the job in the current directory
#$ -cwd

set -x
hostname
ulimit -s unlimited # make sure we can put big arrays on the stack
ulimit -c unlimited # make sure the process can write core dumps
cd /home/marco/LDG-H/trunk/run
export OMP_NUM_THREADS=${num_threads}
date
./../bin/${executable} "${input_file}"
date
EOF

# qsub appends the output, which can be confusing
if [ -e "${job_out}" ]
 then
  rm ${job_out}
fi

qsub ${job_file} && rm ${job_file}

